using System;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace UnityXEditor
{
    [Serializable]
    public class ColdUpdateSetting
    {
        [SerializeField]
        public bool m_Enabled = false;
        [SerializeField]
        public string m_Host = "127.0.0.1";
        [SerializeField]
        public int m_Port = 2022;
        [SerializeField]
        public string m_ExclusionFiles = "";
        [SerializeField]
        public string m_CleanDirectorys = "";

        public void OnGUI()
        {
            m_Enabled = EditorGUILayout.Toggle("启用冷更新功能", m_Enabled);
            EditorGUI.indentLevel++;
            {
                EditorGUI.BeginDisabledGroup(!m_Enabled);
                m_Host = EditorGUILayout.TextField("服务器地址", m_Host);
                m_Port = EditorGUILayout.IntField("服务器端口", m_Port);
                m_ExclusionFiles = EditorGUILayout.TextField(new GUIContent("忽略文件", "占位符：\n[C]:代表当前程序根目录\n[D]:代表Application.dataPath目录\n指定多文件用英文逗号隔开\n如：[C]/xxx.txt,[D]/StreamingAssets/Configs/xxx.txt"), m_ExclusionFiles);
                m_CleanDirectorys = EditorGUILayout.TextField(new GUIContent("更新完清理目录", "占位符：\n[C]:代表当前程序根目录\n[D]:代表Application.dataPath目录\n指定多目录用英文逗号隔开\n如：[C]/MonoBleedingEdge,[D]/StreamingAssets/Configs"), m_CleanDirectorys);
                EditorGUI.EndDisabledGroup();
            }
            EditorGUI.indentLevel--;
        }
        public void Update()
        {
            CodeFileTool.CommentsLine("ColdUpdate.cs", "#define ENABLED", m_Enabled);
            CodeFileTool.CommentsLine("ColdUpdateUI.cs", "#define ENABLED", m_Enabled);

            if (m_Enabled)
            {
                CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static string serverIP =", ";", $"private static string serverIP = \"{UnityXSettings.platformSettings.windows.coldUpdateSetting.m_Host}\";");
                CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static int serverProt =", ";", $"private static int serverProt = {UnityXSettings.platformSettings.windows.coldUpdateSetting.m_Port};");
                if (UnityXSettings.platformSettings.windows.coldUpdateSetting.m_ExclusionFiles != null && UnityXSettings.platformSettings.windows.coldUpdateSetting.m_ExclusionFiles.Length > 0)
                {
                    CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static string exclusionFiles =", ";", $"private static string exclusionFiles = @\"{UnityXSettings.platformSettings.windows.coldUpdateSetting.m_ExclusionFiles}\";");
                }
                else
                {
                    CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static string exclusionFiles =", ";", $"private static string exclusionFiles = null;");
                }
                if (UnityXSettings.platformSettings.windows.coldUpdateSetting.m_CleanDirectorys != null && UnityXSettings.platformSettings.windows.coldUpdateSetting.m_CleanDirectorys.Length > 0)
                {
                    CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static string cleanDirectorys =", ";", $"private static string cleanDirectorys = @\"{UnityXSettings.platformSettings.windows.coldUpdateSetting.m_CleanDirectorys}\";");
                }
                else
                {
                    CodeFileTool.ReplaceLine("ColdUpdate.cs", "private static string cleanDirectorys =", ";", $"private static string cleanDirectorys = null;");
                }
            }
        }
        [PostProcessBuild()]
        public static void PostProcessBuild(BuildTarget target, string path)
        {
            if (target != BuildTarget.StandaloneWindows && target != BuildTarget.StandaloneWindows64)
                return;
            if (!UnityXSettings.platformSettings.windows.coldUpdateSetting.m_Enabled)
                return;

            string sourceDir = EditorFileTool.GetFilePath("ColdUpdateEE");
            string destDir = Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + "_Data/ColdUpdate";
            EditorFileTool.CopyDirectory(sourceDir, destDir);

            File.WriteAllText(destDir + "/Version.ini", PlayerSettings.bundleVersion);
        }
    }
}